/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.contract.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.data.datascope.PigxBaseMapper;
import com.pig4cloud.pigx.contract.bo.ContractExistPositionInfoBO;
import com.pig4cloud.pigx.contract.bo.ContractTradeNewBO;
import com.pig4cloud.pigx.contract.dto.AppContractCurrentPositionConditionDTO;
import com.pig4cloud.pigx.contract.entity.ContractPosition;
import com.pig4cloud.pigx.contract.vo.AppContractCurrentPositionVO;
import com.pig4cloud.pigx.contract.vo.ContractRankingVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * 仓位表
 *
 * @since 2022-08-11 10:08:43
 */
@Mapper
@Component
public interface ContractPositionMapper extends PigxBaseMapper<ContractPosition> {

    /**
     * 开仓时尝试初始化仓位
     *
     * @param list list
     * @return {@link int}
     * @since 2022/8/30 23:27
     */
    int initPositionWhenOpen(List<ContractPosition> list);

    /**
     * 开仓时预查询仓位id
     *
     * @param list list
     * @return {@link List< ContractPosition>}
     * @since 2022/10/11 18:00
     */
    List<ContractPosition> preSelectIdWhenOpen(List<ContractPosition> list);

    /**
     * 查询仓位信息，并锁定
     *
     * @param list list
     * @return {@link List< ContractPosition>}
     * @since 2022/10/8 17:56
     */
    List<ContractPosition> selectPositionWhenTrade(List<ContractTradeNewBO> list);

    /**
     * app查询当前仓位
     *
     * @param condition 查询条件
     * @return {@link List< AppContractCurrentPositionVO>}
     * @since 2022/8/26 21:54
     */
    List<AppContractCurrentPositionVO> appListCurrentPosition(AppContractCurrentPositionConditionDTO condition);

    /**
     * 获取盈利榜
     *
     * @return
     */
    IPage<ContractRankingVO> getProfitRanking(Page page);

    /**
     * 获取亏损榜
     *
     * @return
     */
    IPage<ContractRankingVO> getLossRanking(Page page);

    /**
     * 清算仓位剩余保证金
     *
     * @param id     id
     * @param margin 仓位剩余保证金
     * @return {@link int}
     * @since 2022/8/30 19:09
     */
    int liquidateRestMargin(@Param("id") Long id,
                            @Param("margin") BigDecimal margin);

    /**
     * 增加平仓中数量
     *
     * @param id  id
     * @param num 数量
     * @return {@link int}
     * @since 2022/9/1 17:41
     */
    int addClosingNum(@Param("id") Long id,
                      @Param("num") BigDecimal num);

    /**
     * 减少平仓中数量
     *
     * @param id  id
     * @param num 数量
     * @return {@link int}
     * @since 2022/9/1 17:41
     */
    int minusClosingNum(@Param("id") Long id,
                        @Param("num") BigDecimal num);

    /**
     * 从旧到新查询未发送过邮件的当前可用仓位的用户id集合
     *
     * @param startIndex 起始下标
     * @param endIndex   结束下标
     * @return {@link List< Long>}
     * @since 2022/9/17 18:33
     */
    List<Long> getMarginCheckUserIdList(@Param("startIndex") Integer startIndex,
                                        @Param("endIndex") Integer endIndex);

    /**
     * 根据用户id获取总实现盈亏
     *
     * @param userId
     * @return
     */
    BigDecimal getUserProfit(@Param("userId") Long userId);

    /**
     * 查询已存在的仓位模式、杠杆倍数信息
     *
     * @param userId   用户id
     * @param symbolId 币对id
     * @return {@link ContractExistPositionInfoBO}
     * @since 2022/9/30 10:38
     */
    ContractExistPositionInfoBO existInfo(@Param("userId") Long userId,
                                          @Param("symbolId") Long symbolId);
}
